import java.util.Random;
import java.util.Scanner;

public class test {

    public static void main(String[] args) {
       /* int[] array={1,2,3,4,5};
        int[] array2=new int[]{1,2,3,4,5};
        //长度为5的数组 但是里面的值默认为0
        int[] array3=new int[5];
        array3[0]=11;*/

        //int[] array=null;
        //array={1,2,3,4};  只能和定义写在同一行
        int[] array={1,2,3,4,5};
        System.out.println(array);
        //数组名.length 可以求长度
        System.out.println(array.length);
        System.out.println(array[3]);//越界

        /*int[] array2=null;
        System.out.println(array2.length);//空指针异常*/

        boolean[] flgs=new boolean[10];
    }

    public static void move(char pos1,char pos2){
        System.out.print(pos1+"->"+pos2+" ");
    }

    /**
     *
     *  n 盘子
     *  pos1 起始位置
     *  pos2 中转位置
     *  pos3 目标位置
     */
    public static void hanio(int n,char pos1,char pos2,char pos3){
        if(n==1){
            move(pos1,pos3);
        }else{
            hanio(n-1,pos1,pos3,pos2);
            move(pos1,pos3);
            hanio(n-1,pos2,pos1,pos3);
        }
    }

    public static void main10(String[] args) {
        hanio(1,'A','B','C');
        System.out.println();
        hanio(2,'A','B','C');
        System.out.println();
        hanio(3,'A','B','C');
        System.out.println();
        hanio(4,'A','B','C');
    }

    //斐波那契数1 1 2 3 5 8 13 21
    public static int fib(int n){
        if(n==1||n==2){
            return 1;
        }
        return fib(n-1)+fib(n-2);
    }

    //非递归
    public static int fib2(int n){
        int f1=1;
        int f2=1;
        int f3=0;
        for (int i = 3; i <=n ; i++) {
            f3=f1+f2;
            f1=f2;
            f2=f3;
        }
        return f3;
    }

    //求数每个位上数的和
    public static int sum(int n){
        if(n<10){
            return n;
        }
        return n%10+sum(n/10);
    }

    //从n加到1
    public static int fun1(int n){
        if(n==1){
            return 1;
        }
        return n+fun1(n-1);
    }

    //打印每一位数，从头打印
    public static void fun(int n){
        if(n<10){
            System.out.println(n);
        }else{
            fun(n/10);
            System.out.println(n%10);
        }
    }

    //阶乘
    public static int fac(int n){
        if(n==1){
            return 1;
        }
        return n*fac(n-1);
    }

    public static void main9(String[] args) {
        System.out.println(fib2(40));
    }

    //方法的重载:1.方法名相同 2.参数列表不同 3.与返回值类型是否相同无关
    public static int add(int a,int b){
        return a+b;
    }

    public static double add(double a,double b){
        return a+b;
    }

    public static double add(double a,double b,double c){
        return a+b+c;
    }

    public static void main8(String[] args) {
        int ret1=add(1,2);
        System.out.println(ret1);

        double ret2=add(1.0,2.0);
        System.out.println(ret2);
    }

    //打印用“*”组成的X形图案
    public static void main7(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                    if(i==j||i+j==n-1){
                        System.out.print("*");
                    }else{
                        System.out.print(" ");
                    }
                }
                System.out.println();
            }
        }
    }

    //升级版
    public static int max22(int a,int b) {
        return a > b ? a:b;
    }

    public static int max33(int a,int b,int c) {
        int max = max2(a,b);
        return max > c ? max : c;
    }
    public static void main6(String[] args) {
        System.out.println(max3(2, 5, 1));
    }

    //写两个函数比较三个数的大小
    public static void main5(String[] args) {
        System.out.println("请输入数字：");
        Scanner scan=new Scanner(System.in);
        int a= scan.nextInt();
        int b= scan.nextInt();
        int c= scan.nextInt();
        int max=max3(a,b,c);
        System.out.println(max);
    }

    public static int max2(int a,int b){
        if(a>b){
            return a;
        }else{
            return b;
        }
    }

    public static int max3(int a,int b,int c){
        int max1=max2(c,b);
        int max2=max2(a,c);
        int max=max2(max1,max2);
        return max;
    }
    
    //模拟登入
    public static void main4(String[] args) {
        int count=0;
        String secret="hc123456";
        Scanner scan=new Scanner(System.in);
        while(count<3){
            System.out.println("请输入密码：");
            String in= scan.nextLine();
            //与字符串比较相等格式
            if(in.equals("hc123456")){
                System.out.println("登入成功");
                break;
            }else{
                System.out.println("密码错误");
                count++;
            }
            if(count==3){
                System.out.println("退出程序");
            }
        }
    }

    //计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值
    public static void main3(String[] args) {
        double ret=1.0;
        double sum=0;
        for (int i = 1; i <=100 ; i++) {
            double a=ret/i;
            //System.out.print(ret+"/"+i);
            sum+=a;//注如果是int sum的话，这块sum就为0了
            ret=-ret;
        }
        System.out.println(sum);
    }

    //猜数字
    public static void main2(String[] args) {
        Scanner scan=new Scanner(System.in);
        Random rand=new Random();
        int randNum= rand.nextInt();
        System.out.println("随机数为："+randNum);
        while(true){
            System.out.println("请输入数字：");
            int a= scan.nextInt();
            if(a<randNum){
                System.out.println("猜小了");
            }else if(a==randNum){
                System.out.println("猜对了");
            }else{
                System.out.println("猜大了");
            }
        }
    }

    //多少个数字9
    public static void main1(String[] args) {
        int count=0;
        for (int i = 1; i <=100 ; i++) {
            int tmp=i;
            while(tmp!=0) {
                if (tmp % 10 == 9) {
                    count++;
                    System.out.print(i+" ");
                }
                tmp/=10;
            }
        }
        System.out.println(count);
    }
}
